var grid;
var form;
var form1;
$(function () {

    //加载该页面前，先判断该模块的权限
    //获取当前登录的用户的权限
    var permission = document.cookie.split(";")[1].split("=")[1];
    var b = String(permission).split('');
    console.log(b)
    console.log(b.length)
    var moudle33 = b[9];
    //如果模块权限为1，即只能查询此模块下的数据，不能进行修改（禁用和重置密码不能使用）
    flag = true;
    if (moudle33 == 1) {
        flag = false;
    }
    // ----------------

    var pageNo = 1;
    var pageSize = 5;
    var grid_selector = "#grid-table";
    var pager_selector = "#grid-pager";

    //初始化数据表格
    grid = $(grid_selector).jqGrid({
        datatype: "json",
        url: 'findBlacklistAudit',
        mtype: 'POST',
        page: pageNo,
        postData: {
            'UID': $("#input2").val(),
            'name': $("#input3").val(),
            'source': $("#select1 option:selected").text(),
            'state': $("#select2 option:selected").text(),
        },
        height: 380,
        width: 300,
        colNames: ['id', '证件号码', '名称', '提交时间', '黑名单来源', '提交类型', '操作'],
        colModel: [
            {name: 'id', index: 'id', sortable: false, align: 'center', hidden: true},
            {name: 'UID', index: 'UID', sortable: false, align: 'center'},
            {name: 'name', index: 'name', sortable: false, align: 'center'},
            {name: 'submittime', index: 'submittime', sortable: false, align: 'center'},
            // {name:'founduser',index:'foundUser',sortable:false,align:'center'},
            {name: 'source', index: 'source', sortable: false, align: 'center'},
            {name: 'type', index: 'type', sortable: false, align: 'center'},
            {
                name: 'aaa', index: 'aaa', fixed: true, sortable: false, resize: false,
                formatter: function (cellvalue, options, rowObject) {
                    if (flag == false) {
                        return "<button disabled class='btn btn-warning btn-xs' onclick='openAduit(\"" + rowObject.id + "\")'><i class='fa fa-edit button-in-i'></i>核准</button>"
                            + "<button disabled onclick='openRefuse(\"" + rowObject.id + "\")' class='btn btn-danger btn-xs'><i class='fa fa-trash button-in-i'></i>拒绝</button>";
                    } else {
                        return "<button class='btn btn-warning btn-xs' onclick='openAduit(\"" + rowObject.id + "\")'><i class='fa fa-edit button-in-i'></i>核准</button>"
                            + "<button onclick='openRefuse(\"" + rowObject.id + "\")' class='btn btn-danger btn-xs'><i class='fa fa-trash button-in-i'></i>拒绝</button>";
                    }
                }
            },
        ],
        viewrecords: true,
        rowNum: pageSize,
        rownumbers: false,//添加左侧行号
        rowList: [5, 10, 20],
        pager: pager_selector,
        altRows: true,
        multiselect: false,
        loadComplete: function (data) {
            var table = this;
            setTimeout(function () {
                updatePagerIcons(table);
            }, 0);
            var h = $(window).height() - ($('#grid-pager')[0].offsetHeight + 15) - ($('.result-table .box')[0].offsetTop + $('.content .col-xs-12')[0].offsetTop + $('.ui-jqgrid-bdiv')[0].offsetTop + 3);
            $('.ui-jqgrid-bdiv').height(h);
        },
        autowidth: true,
        jsonReader: jsonReader,   // json中代表实际模型数据的入口
        rownumWidth: 55,
        prmNames: prmNames,
        recordtext: "显示 {0} - {1} 条 , 共 {2} 条",
        emptyrecords: "没有记录",
        loadtext: "正在加载...",
        pgtext: "第 {0} 页  ,共 {1} 页"
    });
    //初始化分页条
    showNavGrid(grid_selector, pager_selector);


    //查询
    $("#queryBtn").click(function () {
        var filters = {
            'UID': $("#input2").val(),
            'name': $("#input3").val(),
            'source': $("#select1 option:selected").text(),
            'state': $("#select2 option:selected").text(),
        }
        grid.jqGrid("setGridParam", {postData: filters, page: 1});
        grid.trigger('reloadGrid');


    });
    //清空
    $("#clearBtn").click(function () {
        $("#input2").val(null).trigger("change");
        $("#input3").val(null).trigger("change");
        $("#select1").val("--全部--").trigger("change");
        $("#select2").val("--请选择--").trigger("change");

    });

});


//拒绝记录
function openRefuse(str) {
    bootbox.confirm("您确定要将删除此行记录吗?", function (result) {
        if (result) {
            $.post("deleteBlacklistAudit", {
                "id": str,
            }, function (data) {
                alert(data.message);
                grid.trigger('reloadGrid');
            });
        }
    });
}

//核准记录
function openAduit(str) {
    bootbox.confirm("您确定要将该用户加入黑名单吗?", function (result) {
        if (result) {
            $.post("updateBlacklistAudit", {
                "id": str,
            }, function (data) {

                alert(data.message);
                grid.trigger('reloadGrid');
            });
        }
    });
}

function checkUID(){
    var UID=$("#input2").val();
    if(!/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9X]$/.test(UID)){

        $("#message").html("身份证格式错误，请重新输入");
        return false;
    }else{
        $("#message").html("身份证格式正确");
    }
}
